package com.wontube.smr.dao.bo;

import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;

import com.wontube.client.modle.vo.PagenateInfo;
import com.wontube.core.dao.mybatis.QueryExample;
import com.wontube.smr.client.model.po.MVChart;
import com.wontube.smr.client.model.vo.MVChartItems;
import com.wontube.smr.dao.mapper.MVChartMapper;
import com.wontube.smr.pub.DefaultDBHelper;

/**
 * music_chart dao帮助类
 * 
 * @author tanggf(2012-4-5)
 * 
 */
public class MVSelectDao {
	private final static int TOTAL_MV_CHART = 100;
	private MVSelectDao() {
	}
	
	public static MVChartItems selectMVCharts(String category, String period, PagenateInfo pageInfo){
		MVChartItems items = new MVChartItems(pageInfo);

		QueryExample ex = new QueryExample();		
		ex.or().addCriterion("mv_chart.category=", category)
				.addCriterion("mv_chart.update_period=", period);
		ex.setOrderByClause("mv_chart.sort_index asc");

		RowBounds rowBounds = new RowBounds(pageInfo.getOffset(), pageInfo.getLimit());
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			MVChartMapper mapper = session.getMapper(MVChartMapper.class);
			List<MVChart> musics = mapper.selectByExample(ex, rowBounds);
			items.setItems(musics);
			items.setTotalCount(TOTAL_MV_CHART);
		} finally {
			session.close();
		}
		return items;
	}
	
	public static List<MVChart> getMVChartsByVideoId(String videoId) {
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			MVChartMapper mapper = session.getMapper(MVChartMapper.class);
			QueryExample ex = new QueryExample();
			ex.or().addCriterion("video_id=", videoId);
			return mapper.selectByExample(ex);
		} finally {
			session.close();
		}
	}
}
